package com.yxgc.chat.wildfire.nativelib;


import android.util.Log;

import com.facebook.react.bridge.Callback;
import com.google.gson.Gson;
import com.yxgc.chat.wildfire.nativelib.util.ErrStatus;

import java.util.Arrays;
import java.util.List;

import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.ConversationInfo;
import cn.wildfirechat.model.UnreadCount;
import cn.wildfirechat.remote.ChatManager;
import cn.wildfirechat.remote.GetConversationListCallback;

class ConversionListModel  {

    private static final List<Conversation.ConversationType> types = Arrays.asList(Conversation.ConversationType.Single,
            Conversation.ConversationType.Group);
    private static final List<Integer> lines = Arrays.asList(0);

    private Gson gson ;
    public  ConversionListModel(){

        gson = new Gson();
    }

    /**
     * 获取聊天列表
     * */
    public void loadConversationList(Callback successCallback, Callback errorCallback) {
        Log.e("yxgc","聊天列表 loadConversationList");
        ChatManager.Instance().getWorkHandler().post(() -> {

            ChatManager.Instance().getConversationListAsync(types, lines, new GetConversationListCallback() {
                @Override
                public void onSuccess(List<ConversationInfo> conversationInfos) {
                    Log.d("yxgc","gson.toJson(conversationInfos) : "+gson.toJson(conversationInfos));
                    successCallback.invoke(gson.toJson(conversationInfos));
                }

                @Override
                public void onFail(int errorCode) {
                    Log.e("yxgc","聊天列表 errorCode : "+errorCode);
                    errorCallback.invoke(ErrStatus.errCallBack(errorCode,"获取聊天列表失败"));
                }
            });
        });
    }


    /**
     * 未读消息
     * */
    public void reloadConversationUnreadStatus(Callback successCallback, Callback errorCallback) {
        ChatManager.Instance().getWorkHandler().post(() -> {
            ChatManager.Instance().getConversationListAsync(types, lines, new GetConversationListCallback() {
                @Override
                public void onSuccess(List<ConversationInfo> conversationInfos) {
                    UnreadCount unreadCount = new UnreadCount();
                    for (ConversationInfo info : conversationInfos) {
                        if (!info.isSilent) {
                            unreadCount.unread += info.unreadCount.unread;
                        }
                        unreadCount.unreadMention += info.unreadCount.unreadMention;
                        unreadCount.unreadMentionAll += info.unreadCount.unreadMentionAll;
                    }
                    successCallback.invoke( gson.toJson(unreadCount));
                }

                @Override
                public void onFail(int errorCode) {
                    successCallback.invoke( ErrStatus.errCallBack(errorCode,"未读消息"));
                }
            });
        });
    }


}
